package es.index.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author BeiBei
 * 
 */
public final class FileUtis {
	private FileUtis() {
	}

	/**
	 * 获取不含后缀的文件名
	 * 
	 * @param name
	 * @return
	 */
	public static String getBaseName(String name) {
		if (name == null)
			return "";
		return new String(name.substring(0, name.lastIndexOf(".")));
	}

	/**
	 * 获取文件后缀名
	 * 
	 * @param name
	 * @return
	 */
	public static String getFileExtension(String name) {
		if (name == null)
			return "";
		return new String(name.substring(name.lastIndexOf(".") + 1));
	}

	/**
	 * 复制文件
	 * 
	 * @param sourceFile
	 *            源文件路径
	 * @param targetFile
	 *            目标文件路径
	 * @throws IOException
	 */
	public static void copyFile(final String sourceFile, final String targetFile)
			throws IOException {
		copyFile(new File(sourceFile), new File(targetFile));
	}

	/**
	 * 复制文件
	 * 
	 * @param sourceFile
	 *            源文件
	 * @param targetFile
	 *            目标文件
	 * @throws IOException
	 */
	public static void copyFile(final File sourceFile, final File targetFile)
			throws IOException {
		// 新建文件输入流并对它进行缓冲
		FileInputStream input = new FileInputStream(sourceFile);
		BufferedInputStream inBuff = new BufferedInputStream(input);

		// 新建文件输出流并对它进行缓冲
		FileOutputStream output = new FileOutputStream(targetFile);
		BufferedOutputStream outBuff = new BufferedOutputStream(output);
		// 缓冲数组
		byte[] b = new byte[1024 * 1024 * 4];
		int len;
		while ((len = inBuff.read(b)) != -1) {
			outBuff.write(b, 0, len);
		}
		// 刷新此缓冲的输出流 outBuff.flush();
		// 关闭流
		inBuff.close();
		outBuff.close();
		output.close();
		input.close();
	}

	/**
	 * 复制文件夹
	 * 
	 * @param sourceDir
	 *            源文件
	 * @param targetDir
	 *            目标文件
	 * @throws IOException
	 */
	public static void copyDirectiory(final String sourceDir,
			final String targetDir) throws IOException {
		// 新建目标目录
		(new File(targetDir)).mkdirs();
		// 获取源文件夹当前下的文件或目录
		File[] file = (new File(sourceDir)).listFiles();
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {
				// 源文件
				File sourceFile = file[i];
				// 目标文件
				File targetFile = new File(
						new File(targetDir).getAbsolutePath() + File.separator
								+ file[i].getName());
				copyFile(sourceFile, targetFile);
			}
			if (file[i].isDirectory()) {
				// 准备复制的源文件夹
				String dir1 = sourceDir + "/" + file[i].getName();
				// 准备复制的目标文件夹
				String dir2 = targetDir + "/" + file[i].getName();
				copyDirectiory(dir1, dir2);
			}
		}
	}

	/**
	 * 删除目录
	 * 
	 * @param path
	 *            目录地址
	 * @return boolean
	 */
	public static boolean delDirectiory(final String path) {
		return delDirectiory(new File(path));
	}

	/**
	 * 删除目录
	 * 
	 * @param file
	 *            目录文件
	 * @return boolean
	 */
	public static boolean delDirectiory(final File file) {
		if (file.isDirectory()) {
			final File[] files = file.listFiles();
			for (final File file2 : files) {
				delDirectiory(file2);
			}
			// System.out.println("Directiory:" + file.getAbsolutePath());
			file.delete();
		} else {
			// System.out.println(file.getAbsolutePath());
			file.delete();
		}
		return false;
	}

	public static String readFileByTxt(final String path) {
		FileReader fr = null;
		BufferedReader br = null;
		try {
			fr = new FileReader(path);
			br = new BufferedReader(fr);
			StringBuffer sbuffer = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null) {
				sbuffer.append(line);
			}
			return sbuffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fr != null)
					fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
